# -*- coding: UTF-8 -*-
"""
@author: tgy
@file: _395_至少有 K 个重复字符的最长子串.py
@time: 2021/03/02
"""


class Solution:

    def longestSubstring(self, s: str, k: int) -> int:
        ...
        if k == 1:
            return len(s)
        self.max_len = 0
        counts = [0]*26
        all_kind = 0
        for i in s:
            loc = ord(i) - ord('a')
            counts[loc] += 1
            if counts[loc] == 1:
                all_kind += 1

        for i in range(1, all_kind + 1):
            ...
            self._do_find(s, k, i)

        return self.max_len

    def _do_find(self, s: str, k: int, kind: int):
        ...
        left = 0
        right = 0
        kd = 0
        counts = [0] * 26
        while right < len(s):
            ...
            loc = ord(s[right]) - ord('a')
            counts[loc]+=1
            if counts[loc] == 1:
                kd += 1
            if kd > kind:
                ...
                while True:
                    loc = ord(s[left]) - ord('a')
                    counts[loc] -= 1
                    left += 1
                    if counts[loc] == 0:
                        kd -= 1
                        break


            if kd == kind:
                flag = True
                for i in range(left, right+1):
                    loc = ord(s[i]) - ord('a')
                    if counts[loc] < k:
                        flag = False
                        break
                if flag:
                    self.max_len = max(right - left + 1,self.max_len)
            right += 1




if __name__ == '__main__':
    pass
    import this
    print('1111')

    # solution = Solution()
    # s = 'aabccbbb'
    # k = 2
    # result = solution.longestSubstring(s,k)
    # print(result)
